#!/bin/sh
get_qos_setting()
{
qos_str=`uci show qos`
. /usr/share/libubox/jshn.sh
json_init

classgroup=`echo "$qos_str" | grep -F "qos.wan.classgroup=" | cut -d '=' -f2`
enabled=`echo "$qos_str" | grep -F "qos.wan.enabled=" | cut -d '=' -f2`
halfduplex=`echo "$qos_str" | grep -F "qos.wan.halfduplex=" | cut -d '=' -f2`
overhead=`echo "$qos_str" | grep -F "qos.wan.overhead=" | cut -d '=' -f2`
upload=`echo "$qos_str" | grep -F "wan.upload=" | cut -d '=' -f2`
download=`echo "$qos_str" | grep -F "wan.download=" | cut -d '=' -f2`

json_add_object wan
json_add_string classgroup "$classgroup"
json_add_string enabled "$enabled"
json_add_string halfduplex "$halfduplex"
json_add_string overhead "$overhead"
json_add_string upload "$upload"
json_add_string download "$download"
json_close_object

classes=`echo "$qos_str" | grep -F "qos.Default.classes=" | cut -d '=' -f2`
default=`echo "$qos_str" | grep -F "qos.Default.default=" | cut -d '=' -f2`
json_add_object Default
json_add_string classes "$classes"
json_add_string default "$default"
json_close_object

json_add_object class
for class in $classes
do

packetsize=`echo "$qos_str" | grep "^qos.${class}.packetsize=" | cut -d '=' -f2`
avgrate=`echo "$qos_str" | grep "^qos.${class}.avgrate=" | cut -d '=' -f2`
priority=`echo "$qos_str" | grep "^qos.${class}.priority=" | cut -d '=' -f2`
json_add_object ${class}
json_add_string packetsize "$packetsize"
json_add_string packetsize "$packetsize"
json_add_string avgrate "$avgrate"
json_close_object

if
echo "$qos_str" | grep -q "qos.${class}_down=class"
then
packetsize=`echo "$qos_str" | grep "^qos.${class}_down.packetsize=" | cut -d '=' -f2`
avgrate=`echo "$qos_str" | grep "^qos.${class}_down.avgrate=" | cut -d '=' -f2`
#priority=`echo "$qos_str" | grep "^qos.${class}_down.priority=" | cut -d '=' -f2`
json_add_object ${class}_down
json_add_string packetsize "$packetsize"
json_add_string packetsize "$packetsize"
json_add_string avgrate "$avgrate"
#json_add_string priority "$priority"
json_close_object
fi
done
json_close_object

classifys=`echo "$qos_str" | grep "qos\..*=classify" | sed -e 's/.*\.//' -e 's/=.*//'`
if
[ -n "$classifys" ]
then
json_add_array classify
for classify in $classifys
do
qos.@classify[0].target=Priority
target=`echo "$qos_str" | grep -F "qos.${classify}.target=" | cut -d '=' -f2`
proto=`echo "$qos_str" | grep -F "qos.${classify}.proto=" | cut -d '=' -f2`
ports=`echo "$qos_str" | grep -F "qos.${classify}.ports=" | cut -d '=' -f2`
comment=`echo "$qos_str" | grep -F "qos.${classify}.comment=" | cut -d '=' -f2`
connbytes=`echo "$qos_str" | grep -F "qos.${classify}.connbytes=" | cut -d '=' -f2`
layer7=`echo "$qos_str" | grep -F "qos.${classify}.layer7=" | cut -d '=' -f2`
srchost=`echo "$qos_str" | grep -F "qos.${classify}.srchost=" | cut -d '=' -f2`
dsthost=`echo "$qos_str" | grep -F "qos.${classify}.dsthost=" | cut -d '=' -f2`

json_add_object ""
json_add_string classify "$classify"
json_add_string target "$target"
json_add_string proto "$proto"
json_add_string ports "$ports"
json_add_string comment "$comment"
json_add_string connbytes "$connbytes"
json_add_string layer7 "$layer7"
json_add_string srchost "$srchost"
json_add_string dsthost "$dsthost"
json_close_object
done

json_close_array
fi
echo ''
json_dump
}
main()
{
. $DOCUMENT_ROOT/apps/home/header.tpl.sh
header_html

enabled=`uci get qos.wan.enabled 2>/dev/null  || echo 0`
halfduplex=`uci get qos.wan.halfduplex 2>/dev/null || echo 0`
overhead=`uci get qos.wan.overhead 2>/dev/null  || echo 0`
download=`uci get qos.wan.download 2>/dev/null`
upload=`uci get qos.wan.upload 2>/dev/null`

cat <<'EOF'
<script type="text/javascript">
function del_rule(rule)
{
if (confirm("你是想删除" + rule + "吗?")) {
	window.location='/index.cgi?app=qos&action=post_del_rule&rule=' + rule;
}}

$(function(){
  $('#post_save_qos_global').on('submit', function(e){
    e.preventDefault();
    var data = "app=qos&action=post_save_qos_global&"+$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'post_save_qos_global');
  });
});
</script>
EOF

cat <<EOF
		<div class="pure-g">
			<div class="l-box-lrg pure-u-1 pure-u-md-1">
				<h3>全局设置</h3>

				<form class="pure-form" id="post_save_qos_global">
				<table class="pure-table pure-table-bordered">
				<tr>
				<td>
				启用
				</td>
				<td>
				<select name="enabled">
				<option value="1" `[ $enabled -eq 1 ] && echo selected`>开</option>
				<option value="0" `[ $enabled -eq 0 ] && echo selected`>关</option>
				</select>
				</td>
				</tr>

				<tr>
				<td>
				半双工
				</td>
				<td>
				<select name="halfduplex">
				<option value="0" `[ $halfduplex -eq 0 ] && echo selected`>关</option>
				<option value="1" `[ $halfduplex -eq 1 ] && echo selected`>开</option>
				</select>
				</td>
				</tr>

				<tr>
				<td>
				速率限制
				</td>
				<td>
				<select name="overhead">
				<option value="0" `[ $overhead -eq 0 ] && echo selected`>关</option>
				<option value="1" `[ $overhead -eq 1 ] && echo selected`>开</option>
				</select>
				</td>
				</tr>

				<tr>
				<td>
				默认队列
				</td>
				<td>
				<select name="default_class">
EOF
				default_class=`uci get qos.Default.default`
				name_Priority="最高"
				name_Express="高"
				name_Normal="普通"
				name_Bulk="低"
				for class in `uci show qos | grep "=class$" | grep -v "_down" | sed -e 's/qos\.//' -e 's/=class//'`
				do
				name=`eval echo '$name_'${class}`
				[ -z "$name" ] && name="${class}"
				cat <<EOF
				<option value="${class}" `[ "$default_class" = "${class}" ] && echo selected`>$name</option>
EOF
				done
				cat <<EOF
				</select>
				</td>
				</tr>

				<tr>
				<td>
				下载速度
				</td>
				<td>
				<input type="text" name="download" value="$download"></input>
				</td>
				</tr>

				<tr>
				<td>
				上传速度
				</td>
				<td>
				<input type="text" name="upload" value="$upload"></input>
				</td>
				</tr>

				<tr>
				<td>
				操作
				</td>
				<td>
				<button class="pure-button" type="submit">保存</button>
				</td>
				</tr>
				</table>
				</form>
			</div>
		</div>
EOF


cat <<EOF
		<div class="pure-g">
			<div class="l-box-lrg pure-u-1 pure-u-md-1">
				<h3>规则管理</h3>
				<table class="pure-table pure-table-bordered">
EOF

				for rule in `uci -X show qos | grep "=classify$" | sed -e 's/qos\.//' -e 's/=classify//'`
				do
				cat <<EOF
<script>
\$(function(){
  \$('#post_save_rule_${rule}').on('submit', function(e){
    e.preventDefault();
    var data = "app=qos&action=post_save_rule&"+\$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'post_save_rule_${rule}');
  });
});
</script>
EOF
				cat <<EOF
				<form id="post_save_rule_${rule}">
				<tr>
				<td>
				<div>
				<label>注释</label>
				<input type="text" name="comment" value="`uci get qos.${rule}.comment 2>/dev/null`"></input>
				</div>
				<div>
				<label>队列</label>
				<select name="target">
EOF
				name_Priority="最高"
				name_Express="高"
				name_Normal="普通"
				name_Bulk="低"
				for class in `uci show qos | grep "=class$" | grep -v "_down" | sed -e 's/qos\.//' -e 's/=class//'`
				do
				name=`eval echo '$name_'${class}`
				[ -z "$name" ] && name="${class}"
				cat <<EOF
				<option value="${class}" `[ "${class}" = "$(uci get qos.${rule}.target 2>/dev/null)" ] && echo selected`>$name</option>
EOF
				done
				cat <<EOF
				</select>
				</div>
				</td>
				<td>
				<div>
				<label>来源主机</label>
				<input type="text" name="srchost" value="`uci get qos.${rule}.srchost 2>/dev/null || echo all`"></input>
				</div>
				<div>
				<label>目的主机</label>
				<input type="text" name="dsthost" value="`uci get qos.${rule}.dsthost 2>/dev/null || echo all`"></input>
				</div>
				</td>
				<td>
				<div>
				<label>服务</label>
				<select name="layer7">
				<option `[ -z "$(uci get qos.${rule}.layer7)" ] && echo selected`>all</option>
EOF
				L7_proto=`find /etc/l7-protocols/ -type f | sed -e 's#.*/##g' -e 's/\.pat//'`
				for service in $L7_proto
				do
				cat <<EOF
				<option value="${service}" `[ "$(uci get qos.${rule}.layer7 2>/dev/null)" = "${service}" ] && echo selected`>${service}</option>
EOF
				done
				cat <<EOF
				</select>
				</div>
				<div>
				<label>协议</label>
				<input list="proto" name="proto" value="`uci get qos.${rule}.proto || echo all`">
				<datalist id="proto">
				<option value="all">
				<option value="tcp">
				<option value="udp">
				<option value="icmp">
				</datalist>
				</div>
				</td>
				<td>
				<div>
				<label>端口</label>
				<input type="text" name="ports" value="`uci get qos.${rule}.ports 2>/dev/null || echo all`"></input>
				</div>
				<div>
				<label>字节数</label>
				<input type="text" name="connbytes" value="`uci get qos.${rule}.connbytes 2>/dev/null`"></input>
				</div>
				</td>
				<td>
				<button class="pure-button button-error" type="button" onclick="del_rule('${rule}')">删除</button>
				<input type="hidden" name="rule" value="${rule}"></input>
				<button class="pure-button" type="sumbmit">保存</button>
				</td>
				</tr>
				</form>

EOF
				done
				cat <<EOF
<script>
\$(function(){
  \$('#post_save_rule_new').on('submit', function(e){
    e.preventDefault();
    var data = "app=qos&action=post_save_rule&"+\$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'post_save_rule_new');
	setTimeout("window.location.reload();", 5000);
  });
});
</script>
EOF
				# 添加规则
				cat <<EOF
				<form id="post_save_rule_new">
				<tr>
				<td>
				<div>
				<label>注释</label>
				<input type="text" name="comment" value=""></input>
				</div>
				<div>
				<label>队列</label>
				<select name="target">
EOF
				name_Priority="最高"
				name_Express="高"
				name_Normal="普通"
				name_Bulk="低"
				for class in `uci show qos | grep "=class$" | grep -v "_down" | sed -e 's/qos\.//' -e 's/=class//'`
				do
				name=`eval echo '$name_'${class}`
				[ -z "$name" ] && name="${class}"
				cat <<EOF
				<option value="${class}">$name</option>
EOF
				done
				cat <<EOF
				</select>
				</div>
				</td>
				<td>
				<div>
				<label>来源主机</label>
				<input type="text" name="srchost" value=""></input>
				</div>
				<div>
				<label>目的主机</label>
				<input type="text" name="dsthost" value=""></input>
				</div>
				</td>
				<td>
				<div>
				<label>服务</label>
				<select name="layer7">
				<option >all</option>
EOF
				# L7_proto=`find /etc/l7-protocols/ -type f | sed -e 's#.*/##g' -e 's/\.pat//'`
				for service in $L7_proto
				do
				cat <<EOF
				<option value="${service}" >${service}</option>
EOF
				done
				cat <<EOF
				</select>
				</div>
				<div>
				<label>协议</label>
				<input list="proto" name="proto" value="">
				<datalist id="proto">
				<option value="all">
				<option value="tcp">
				<option value="udp">
				<option value="icmp">
				</datalist>
				</div>
				</td>
				<td>
				<div>
				<label>端口</label>
				<input type="text" name="ports" value=""></input>
				</div>
				<div>
				<label>字节数</label>
				<input type="text" name="connbytes" value=""></input>
				</div>
				</td>
				<td>
				<input type="hidden" name="rule" value="new"></input>
				<button class="pure-button button-secondary" type="sumbmit">添加规则</button>
				</td>
				</tr>
				</form>
				</table>
			</div>
		</div>
EOF


footer_html
}

post_save_qos_global()
{
uci set qos.wan.enabled=$FORM_enabled
uci set qos.wan.halfduplex=$FORM_halfduplex
uci set qos.wan.overhead=$FORM_overhead
uci set qos.wan.download=$FORM_download
uci set qos.wan.upload=$FORM_upload
uci set qos.Default.default=$FORM_default_class
echo "QOS全局修改成功!!" | main.sbin output_json 0
}
post_save_rule()
{
main.sbin qos_save_rule
echo "QOS规则修改成功!!" | main.sbin output_json 0
}
post_del_rule()
{
uci set qos.$FORM_rule=""
uci commit qos
/etc/init.d/qos restart >/dev/null 2>&1
main.sbin go_success
}

lang=`main.sbin get_client_lang`
eval `cat $DOCUMENT_ROOT/apps/$FORM_app/i18n/$lang/i18n.conf`
if
[ $is_main_page = 1 ]
then
main
elif [ -n "$FORM_action" ]
then
$FORM_action
fi
